#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244353;
typedef long long LL;

void solve(){
    int q;
    cin >> q;
    int ans = 0;
    int sum = 0;
    deque<int>qx;
    int opx = 0;
    int pos = 0;
    while(q --)
    {
        int op;
        cin >> op;
        if(op == 1)
        {   
            if(opx == 0)
            {
                int u = qx.back();
                qx.pop_back();
                qx.push_front(u);
                ans += sum - qx.size() * u;
            }
            else
            {
                int u = qx.front();
                qx.pop_front();
                qx.push_back(u);
                ans += sum - qx.size() * u;
            }
        }
        else if(op == 2)
        {
            int sup = (qx.size() + 1) * sum;
            ans = sup - ans;
            if(opx == 0) opx = 1;
            else opx = 0;
        }
        else
        {
            int x;
            cin >> x;
            sum += x;
            if(opx == 0) qx.push_back(x);
            else qx.push_front(x);
            ans += qx.size() * x;
        }
        cout << ans << endl;
    }
}

signed main(){
    int t = 1;
    cin >> t;
    while(t--)
    {
        solve();
    }
}


